// 导入db模块
const db = require('../config/db');
// 数据表：db_parking_lot
// 停车场信息类
class ParkingLot {
    // 获取停车场信息
    info() {
        return new Promise((resolve, reject) => {
            // 查询账号
            let sql = `select * from db_parking_lot`;
            db.query(sql)
                .then(
                    (rows) => {
                        // console.log('y.2',rows);
                        if (rows) {
                            resolve({
                                code: 200,
                                msg: '获取停车场信息成功。',
                                data: rows
                            })
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '获取停车场信息失败。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询车辆失败。'
                        })
                    }
                )
        })
    }
    // 入场减少一辆车
    sub() {
        return new Promise((resolve, reject) => {
            // 查询账号
            let sql = `select * from db_parking_lot`;
            db.query(sql)
                .then(
                    (rows) => {
                        // console.log('y.2',rows);
                        if (rows) {
                            let parking_lot_count = rows[0].parking_lot_count;
                            parking_lot_count --;
                            if(parking_lot_count < 0) {
                                parking_lot_count = 0;
                                resolve({
                                    code: 200,
                                    msg: '该停车场没有剩余车位了',
                                    parking_lot_count
                                })
                                return false;
                            }
                            // 修改剩余车位
                            let sql2 = `update db_parking_lot set parking_lot_count= '${parking_lot_count}'`;
                            db.query(sql2)
                                .then(
                                    (result) => {
                                        // 判断是否修改成功
                                        if (result.affectedRows == 1) {
                                            resolve({
                                                code: 200,
                                                msg: '设置剩余车位成功。',
                                                parking_lot_count
                                            })
                                        }
                                        else {
                                            resolve({
                                                code: -1,
                                                msg: '设置剩余车位失败。'
                                            })
                                        }

                                    },
                                    (err) => {
                                        reject({
                                            code: -1,
                                            msg: '服务端发生异常。~~~',
                                            err
                                        })
                                    }
                                )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '获取停车场信息失败。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询车辆失败。'
                        })
                    }
                )
        })
    }
    // 离场增加一辆车
    add() {
        return new Promise((resolve, reject) => {
            // 查询账号
            let sql = `select * from db_parking_lot`;
            db.query(sql)
                .then(
                    (rows) => {
                        // console.log('y.2',rows);
                        if (rows) {
                            let parking_lot_count = rows[0].parking_lot_count;
                            parking_lot_count ++;
                            if(parking_lot_count > rows[0].parking_lot_total) {
                                parking_lot_count = rows[0].parking_lot_total;
                                resolve({
                                    code: 200,
                                    msg: '剩余车位数量充足',
                                    parking_lot_count
                                })
                                return false;
                            }
                            // 修改剩余车位
                            let sql2 = `update db_parking_lot set parking_lot_count= '${parking_lot_count}'`;
                            db.query(sql2)
                                .then(
                                    (result) => {
                                        // 判断是否修改成功
                                        resolve({
                                            code: 200,
                                            msg: '设置剩余车位成功。',
                                            parking_lot_count
                                        })
                                    },
                                    (err) => {
                                        reject({
                                            code: -1,
                                            msg: '服务端发生异常。~~~',
                                            err
                                        })
                                    }
                                )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '获取停车场信息失败。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询车辆失败。'
                        })
                    }
                )
        })
    }
    // 停车场收入
    income(money) {
        return new Promise((resolve, reject) => {
            // 查询账号
            let sql = `select * from db_parking_lot`;
            db.query(sql)
                .then(
                    (rows) => {
                        // console.log('y.2',rows);
                        if (rows) {
                            let parking_log_income = rows[0].parking_log_income;
                            parking_log_income =(parking_log_income * 100) + (money - 0) * 100
                            parking_log_income = parking_log_income/ 100;
                           // 修改收入信息
                           let sql2 = `update db_parking_lot set parking_log_income= '${parking_log_income}'`;
                           db.query(sql2)
                               .then(
                                   (result) => {
                                       // 判断是否修改成功
                                       resolve({
                                           code: 200,
                                           msg: '收款成功。',
                                           parking_log_income
                                       })
                                   },
                                   (err) => {
                                       reject({
                                           code: -1,
                                           msg: '服务端发生异常。~~~',
                                           err
                                       })
                                   }
                               )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '获取停车场信息失败。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询车辆失败。'
                        })
                    }
                )
        })
    }
}


// 暴露停车场类实例
module.exports = new ParkingLot();